﻿
using System.Collections.Generic;

namespace LxCFD
{
    public class ResidualMonitor : MonitorBase
    {
        protected List<ICalResidual> Residuals = new List<ICalResidual>();
        public override void Reset(ResetTypes clearType)
        {
            base.Reset(clearType);
            switch (clearType)
            {
                case ResetTypes.ClearToLoadSimulation:
                case ResetTypes.ClearToLoadMesh:
                case ResetTypes.ClearFields:
                case ResetTypes.UpdateSolveCenter:
                    Residuals.Clear();
                    break;
                case ResetTypes.None:
                default:
                    break;
            }
        }
        public void Add(ICalResidual calResidual)
        {
            Residuals.Add(calResidual);
        }

        public override void RunMonitor()
        {
            if (Residuals.Count > 0)
            {
                double normalRc = 0;
                foreach (var s in Residuals)
                {
                    s.GetResidual_Reset(out double Rc, out double acPhic);
                    if (acPhic == 0) continue;
                    double local = Rc / acPhic;
                    if (local > normalRc)
                    {
                        normalRc = local;
                    }
                }
                CurrentResult = normalRc;
                IsCurrentExist = true;
            }
            else
            {
                IsCurrentExist = false;
                CurrentResult = 0;
            }
        }
    }
}
//public double GetMaxNormalRc<T>(List<T> scalars) where T : ICalResidual
//{
//    double normalRc = 0;
//    foreach (var s in scalars)
//    {
//        s.GetResidual_Reset(out double Rc, out double acPhic);
//        if (acPhic == 0) continue;
//        double local = Rc / acPhic;
//        if (local > normalRc)
//        {
//            normalRc = local;
//        }
//    }
//    return normalRc;
//}
//MultiThreads.RunCalTasks((taskIndex) =>
//{
//    foreach (var r in Residuals)
//    {
//        r.CalResidual(taskIndex);
//    }
//});
